/**
 * <p>Title: ServiceZP17.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2012-10-17 上午07:24:07  张志芳  Create
 *</pre>
 */
package com.baosight.sczx.zp.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.FrameworkInfo;
import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.QueryMap;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.iplat4j.es.domain.ESPost;
import com.baosight.iplat4j.es.domain.ESUser;
import com.baosight.iplat4j.security.bridge.SecurityBridge;
import com.baosight.iplat4j.security.bridge.SecurityBridgeFactory;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zp.ZPUtils;
import com.baosight.sczx.zp.domain.ZPUserset;

/**
 * 用户权限管理
 * <p>Title: ServiceZP17.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2012-10-17 上午07:24:07  张志芳  Create
 *</pre>
 */
public class ServiceZP17 extends ServiceEPBase {

	SecurityBridge bridge = SecurityBridgeFactory.getBridge();
	
	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP14</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-8上午07:59:00 张志芳
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo inInfo)
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP14</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-8上午07:59:12 张志芳
	 * </p>
	 *
	 */
	public EiInfo query(EiInfo inInfo)
	{
		String orgCode = inInfo.getString("orgCode");
		if(orgCode != null && (!ZPUtils.USER_COMPANY_ID.equals(orgCode)))
		{
			StaticTools.setQueryParameters(inInfo, "orgCode", orgCode, true);
		}
		StaticTools.setQueryParameters(inInfo, "projectPrefix", FrameworkInfo.getProjectPrefix(), true);
		EiInfo outInfo = super.query(inInfo, "ZPUser.queryUser", new ESUser());
		return outInfo;
	}
	
	/**
	 * 添加角色
	 * <p>Function: add</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-17上午07:58:38 张志芳
	 * </p>
	 *
	 */
	public EiInfo add(EiInfo inInfo)
	{
		String userId = inInfo.getString("userId");
		
		for(int n=0; n<inInfo.getBlock(EiConstant.resultBlock).getRowCount(); n++)
		{
			String role = inInfo.getBlock(EiConstant.resultBlock).getCellStr(n, "postLabel");
			bridge.addUserToRole(role, userId);
		}
		
		return inInfo;
	}
	
	/**
	 * 删除角色
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP17</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-17上午08:21:19 张志芳
	 * </p>
	 *
	 */
	public EiInfo delete(EiInfo inInfo)
	{
		String userId = inInfo.getString("userId");
		for(int n=0; n<inInfo.getBlock(EiConstant.resultBlock).getRowCount(); n++)
		{
			String role = inInfo.getBlock(EiConstant.resultBlock).getCellStr(n, "postLabel");
			bridge.removeUserFromRole(role, userId);
		}
		
		return inInfo;
	}
	
	/**
	 * 查询角色
	 * <p>Function: queryRoles</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-17上午08:48:44 张志芳
	 * </p>
	 *
	 */
	public EiInfo queryRoles(EiInfo inInfo)
	{
		//提取查询条件
		QueryMap queryMap = QueryMap.getQueryMap(inInfo);
		Map map = new HashMap();
		map.put("orgLabel", ZPUtils.USER_COMPANY_ID);
		map.put("postTypeLabel", queryMap.get("postTypeLabel"));
		map.put("postTypeName", queryMap.get("postTypeName"));
		List<ZPUserset> list = dao.query("ZPUserset.query", map);
		
		//构造返回值outInfo
		EiInfo outInfo = new EiInfo();
		outInfo.setAttr(inInfo.getAttr());
		outInfo.setBlock(inInfo.getBlock(EiConstant.queryBlock));
		EiBlock block = outInfo.addBlock("rolesResult");
		block.setBlockMeta(new ZPUserset().eiMetadata);
		
		//去除已经添加过的
		String userId = inInfo.getString("userId");
		if(userId!=null){
			List<ESPost> list1 = bridge.getUserRoles(userId, "");
			for(int n=0; n<list.size(); n++){
				Boolean flag = true;
				for(int m=0; m<list1.size(); m++){
					if(list.get(n).getPostLabel().equals(list1.get(m).getPostLabel())){
						flag = false;
						break;
					}
				}
				if(flag){
					block.addRow(list.get(n));
				}
			}
		}
		else {
			block.setRows(list);
		}
		return outInfo;
	}
	
	/**
	 * 查询要删除的角色
	 * <p>Function: queryRoles</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-17上午08:48:44 张志芳
	 * </p>
	 *
	 */
	public EiInfo queryDelRoles(EiInfo inInfo)
	{
		//构造返回值outInfo
		EiInfo outInfo = new EiInfo();
		outInfo.setAttr(inInfo.getAttr());
		outInfo.setBlock(inInfo.getBlock(EiConstant.queryBlock));
		EiBlock block = outInfo.addBlock("rolesResult");
		block.setBlockMeta(new ZPUserset().eiMetadata);
		
		String userId = inInfo.getString("userId");
		List<ESPost> list = bridge.getUserRoles(userId, "");
		for(int n=0; n<list.size(); n++){
			ZPUserset user = new ZPUserset();
			user.setPostLabel(list.get(n).getPostLabel());
			user.setOrgLabel(list.get(n).getOrgLabel());
			user.setPostTypeLabel(list.get(n).getPostTypeLabel());
			user.setPostTypeName(bridge.getRoleTypeDisplayName(list.get(n).getPostTypeLabel()));
			block.addRow(user);
		}
		
		return outInfo;
	}
}
